local skel = fk.CreateSkill{
  name = "rmt__ninglan",
}

Fk:loadTranslationTable{
  ["rmt__ninglan"] = "凝澜",
  [":rmt__ninglan"] = "你可以分配1点冰冻伤害，视为使用无法被响应的【无懈可击】；若你本轮已受到过伤害，你须选择触发弃牌效果；若你本轮已使用过【无懈可击】，你须将伤害分配给自己。",

  ["#rmt__ninglan"] = "凝澜：你可%arg分配1点%arg2冰冻伤害，视为使用【无懈可击】",
  ["rmt__ninglan_self"] = "对自己",
  ["rmt__ninglan_mustthrow"] = "必须弃牌的",
  ["#rmt__ninglan-damage"] = "凝澜：分配1点冰冻伤害",
}

skel:addEffect("viewas", {
  anim_type = "defensive",
  pattern = "nullification",
  prompt = function (self, player)
    local arg = player:getMark("rmt__ninglan_nullification-round") ~= 0 and "rmt__ninglan_self" or ""
    local arg2 = player:getMark("rmt__ninglan_damaged-round") ~= 0 and "rmt__ninglan_mustthrow" or ""
    return "#rmt__ninglan:::" .. arg .. ":" .. arg2
  end,
  view_as = function (self, player, cards)
    local card = Fk:cloneCard("nullification")
    card.skillName = skel.name
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local tos = room:getAlivePlayers(false)
    use.disresponsiveList = room:getAlivePlayers(false)
    local to = player
    if player:getMark("rmt__ninglan_nullification-round") == 0 then
      to = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        prompt = "#rmt__ninglan-damage",
        skill_name = skel.name,
        cancelable = false
      })[1]
    end
    local dmg = {
      from = player,
      to = to,
      damage = 1,
      damageType = fk.IceDamage,
      skillName = skel.name,
    }
    if player:getMark("rmt__ninglan_damaged-round") ~= 0 then
      dmg.extra_data = dmg.extra_data or {}
      dmg.extra_data.rmt__ninglan_mustthrow = true
    end
    room:damage(dmg)
  end
})

-- 为了宝宝提示做的记录
skel:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function(self, event, target, player, data)
    return player == target and data.card.trueName == "nullification" and player:getMark("rmt__ninglan_nullification-round") == 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "rmt__ninglan_nullification-round", 1)
  end,
})

skel:addEffect(fk.Damaged, {
  can_refresh = function(self, event, target, player, data)
    return player == target and player:getMark("rmt__ninglan_damaged-round") == 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "rmt__ninglan_damaged-round", 1)
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  if #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
    return e.data.from == player
    and e.data.card.name == "nullification"
  end, Player.HistoryRound) == 1 then
    room:setPlayerMark(player, "rmt__ninglan_nullification-round", 1)
  end
  if #room.logic:getActualDamageEvents(1, function(e)
    return e.data.to == player
  end, Player.HistoryRound) == 1 then
    room:setPlayerMark(player, "rmt__ninglan_damaged-round", 1)
  end
end)

skel:addEffect(fk.DamageCaused, {
  priority = 0.02, -- 优先级调快点不就好了
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.damageType == fk.IceDamage and
      data.extra_data and data.extra_data.rmt__ninglan_mustthrow
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data:preventDamage()
    for _ = 1, 2 do
      if data.to:isNude() or data.to.dead or player.dead then break end
      local card = room:askToChooseCard(player, {
        target = data.to,
        flag = "he",
        skill_name = "ice_damage_skill",
      })
      room:throwCard(card, "ice_damage_skill", data.to, player)
    end
  end,
})

--- 笑死。没用
--[[
skel:addEffect(fk.BeforeRequestAsk, {
  can_refresh = function (self, event, target, player, data)
    if player.seat == 1 and data and data.command == "AskForSkillInvoke" and type(data.data) == "table"
    and type((data.data)[1])== "table"
    and ((data.data)[1])[1] == "ice_damage_skill" then
      local currentEvent = player.room.logic:getCurrentEvent()
      if currentEvent.event == GameEvent.Damage then
        local dmg = currentEvent.data
        return dmg.extra_data and dmg.extra_data.rmt__ninglan_mustthrow
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local p = data.players[1]
    data.result[p.id] = "1"
  end,
})
--]]

return skel
